package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDeviceList;
import com.ruoyi.system.mapper.SysDeviceListMapper;
import com.ruoyi.system.service.ISysDeviceListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备清单 业务层处理
 * 
 * @author ljf
 */
@Service
public class SysDeviceListServiceImpl implements ISysDeviceListService
{
    private static final Logger log = LoggerFactory.getLogger(SysDeviceListServiceImpl.class);

    @Autowired
    private SysDeviceListMapper sysDeviceListMapper;

    /**
     * 根据条件分页查询用户列表
     * 
     * @param sysDeviceList 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<SysDeviceList> selectDeviceList(SysDeviceList sysDeviceList)
    {
        return sysDeviceListMapper.selectDeviceList(sysDeviceList);
    }

    /**
     * 新增保存用户信息
     * 
     * @param sysDeviceList 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDeviceList(SysDeviceList sysDeviceList)
    {
        // 新增用户信息
        int rows = sysDeviceListMapper.insertDeviceList(sysDeviceList);
        return rows;
    }

    /**
     * 修改设备清单信息
     * 
     * @param sysDeviceList 设备清单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDeviceList(SysDeviceList sysDeviceList)
    {
        return sysDeviceListMapper.updateDeviceList(sysDeviceList);
    }

    /**
     * 批量删除设备清单信息
     * 
     * @param listIds 需要删除的设备清单ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDeviceListByIds(Long[] listIds)
    {
        return sysDeviceListMapper.deleteDeviceListByIds(listIds);
    }

    /**
     * 导入用户数据
     *
     * @param deviceList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importDeviceList(List<SysDeviceList> deviceList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(deviceList) || deviceList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysDeviceList deviceListinfo : deviceList)
        {
            try
            {
                // 验证是否存在这个设备
                SysDeviceList d = sysDeviceListMapper.selectListByNameAndId(deviceListinfo);
                if (StringUtils.isNull(d))
                {
                    deviceListinfo.setCreateBy(operName);
                    this.insertDeviceList(deviceListinfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备 " + deviceListinfo.getDeviceName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    deviceListinfo.setUpdateBy(operName);
                    this.updateDeviceList(deviceListinfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备 " + deviceListinfo.getDeviceName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、设备 " + deviceListinfo.getDeviceName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备 " + deviceListinfo.getDeviceName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
